/**
 * 
 */
package domain;
import java.util.*;

import data.IArticleDAO;
import data.IProductDAO;
import data.JDBCArticleDAO;
import data.JDBCProductDAO;

/**
 * @author it-equipment-selection team
 *
 */
public class Store {

	private static Store s;
	private Collection products;
	private Collection articles;
	private String userProfile;
	private IQuery customQuery;
	
	protected Store (){
		/*Product p1, p2, p3, p4, p5, p6;
		Article printer = new Article("Impresora","2345","25/ISE1/2007");
		Article computer = new Article("Ordenador","4562","25/ISE1/2007");
		Article laptop = new Article("Portatil","2112","25/ISE1/2007");
		Supplier hp = new Supplier(1023,"Las Rozas, Madrid","HP España","zonasur.comercial@hp.es",0.9);
		Supplier sadiel = new Supplier(812,"Isla de la Cartuja, Sevilla","Sadiel S.A.","comercial@sadiel.es",0.8);
		p1 = new LocalPrinter("Samsung","ML-1510", 88.50, printer, sadiel, 8, false, false, false, true);
		p2 = new LocalPrinter("HP","Laserjet 1020", 98.15, printer, hp, 10, false, false, false, false);
		p3 = new Computer("HP","Pavilion m8175", 281.90, computer, hp, "AMD 64", 2048, 80, 19, false);
		p4 = new Laptop("Toshiba","Satellite A-200", 599.90, laptop, sadiel, "Intel Centrino", 1024, 160, 15.4, true, 400, 2.5 );
		p5 = new Laptop("Toshiba","Tecra m5", 1599.90, laptop, sadiel, "Intel Core Solo", 1024, 160, 15.4, true, 480, 3.5 );
		p6 = new Laptop("Toshiba","Satellite A-210", 699.90, laptop, sadiel, "Intel Core Duo", 2048, 250, 15.4, true, 600, 2.1 );
		products = new ArrayList();
		products.add(p1);
		products.add(p2);
		products.add(p3);
		products.add(p4);
		products.add(p5);
		products.add(p6);
		articles = new ArrayList();
		articles.add(printer);
		articles.add(computer);
		articles.add(laptop);*/
		getProducts();
		getArticles();
	}
	
	public Collection getProducts(){
		IProductDAO dao = new JDBCProductDAO(); //ojo acoplamiento -> dao.propierties
		products = dao.selectAllProducts();
		return products;
	}
	
	public void addProduct(Product p){
		IProductDAO dao = new JDBCProductDAO(); //ojo acoplamiento -> dao.propierties
		dao.insert(p);
		products.add(p);
	}
	
	public Collection getArticles(){
		IArticleDAO dao = new JDBCArticleDAO(); //ojo acoplamiento -> dao.propierties
		articles = dao.selectAllArticles();
		return articles;
	}
	
	public void addArticle(Article a){
		articles.add(a);
	}
	
	public void setUserProfile(User u){
		if (u.getProfile().equals("tecnico")){
			this.userProfile = "technician";
		} else if (u.getProfile().equals("licitador")){
			this.userProfile = "supplier";
		} else if (u.getProfile().equals("mesa")){
			this.userProfile = "contract_table";
		} else {
			this.userProfile = "user";
		}
	}
	
	public Collection performQuery(Collection p, Collection a){
		Collection aux;
		if (this.userProfile == "technician"){
			this.customQuery = new TechnicianQuery();
			aux = customQuery.productQuery(p,a);
		} else if (this.userProfile == "supplier"){
			this.customQuery = new SupplierQuery();
			aux = customQuery.productQuery(p,a);
		} else if (this.userProfile == "contract_table"){
			this.customQuery = new ContractTableQuery();
			aux = customQuery.productQuery(p,a); 
		} else {
			this.customQuery = new UserQuery();
			aux = customQuery.productQuery(p,a);
		}
		return aux;
	}

	public static synchronized Store getInstance(){
		if (s == null) {
			s = new Store();
		}
		return s;
	}
}
